// 升级系统 - 管理玩家升级和升级选项
class UpgradeSystem {
    constructor(scene) {
        this.scene = scene;
        this.refreshCost = 10; // 刷新奖励的金币消耗
        this.refreshCostMultiplier = 1.5; // 每次刷新后价格倍数
    }

    generateUpgradeOptions() {
        const allUpgrades = [
            // === 核心属性升级 ===
            {
                name: '生命强化',
                description: '最大生命值 +25，完全治愈',
                rarity: 'common',
                effect: () => {
                    this.scene.playerStats.maxHealth += 25;
                    this.scene.playerStats.health = this.scene.playerStats.maxHealth;
                    if (this.scene.uiSystem) {
                        this.scene.uiSystem.updateHealthText(this.scene.playerStats.health);
                    }
                }
            },
            {
                name: '法力觉醒',
                description: '最大法力值 +20，法力回复 +1/秒',
                rarity: 'uncommon',
                effect: () => {
                    this.scene.playerStats.maxMana += 20;
                    this.scene.playerStats.mana = this.scene.playerStats.maxMana;
                    this.scene.playerStats.manaRegen += 1;
                }
            },
            {
                name: '能量充盈',
                description: '最大能量值 +30，能量回复 +1/秒',
                rarity: 'common',
                effect: () => {
                    this.scene.playerStats.maxEnergy += 30;
                    this.scene.playerStats.energy = this.scene.playerStats.maxEnergy;
                    this.scene.playerStats.energyRegen += 1;
                }
            },

            // === 主要属性升级 ===
            {
                name: '力量训练',
                description: '力量 +3，物理伤害 +2',
                rarity: 'common',
                effect: () => {
                    this.scene.playerStats.strength += 3;
                    this.scene.playerStats.physicalDamage += 2;
                }
            },
            {
                name: '敏捷提升',
                description: '敏捷 +3，暴击率 +3%，移动速度 +5%',
                rarity: 'common',
                effect: () => {
                    this.scene.playerStats.agility += 3;
                    this.scene.playerStats.critChance += 0.03;
                    this.scene.playerStats.moveSpeed += 0.05;
                }
            },
            {
                name: '智力增长',
                description: '智力 +3，魔法伤害 +2，技能冷却 -5%',
                rarity: 'common',
                effect: () => {
                    this.scene.playerStats.intelligence += 3;
                    this.scene.playerStats.magicalDamage += 2;
                    this.scene.playerStats.skillCooldownReduction += 0.05;
                }
            },
            {
                name: '体力强化',
                description: '体力 +3，最大生命值 +15，防御 +2',
                rarity: 'common',
                effect: () => {
                    this.scene.playerStats.vitality += 3;
                    this.scene.playerStats.maxHealth += 15;
                    this.scene.playerStats.defense += 2;
                }
            },
            {
                name: '幸运祝福',
                description: '幸运 +3，掉落率 +10%，暴击率 +2%',
                rarity: 'uncommon',
                effect: () => {
                    this.scene.playerStats.luck += 3;
                    this.scene.playerStats.dropRateBonus += 0.1;
                    this.scene.playerStats.critChance += 0.02;
                }
            },

            // === 战斗属性升级 ===
            {
                name: '伤害提升',
                description: '基础伤害 +6',
                rarity: 'common',
                effect: () => { this.scene.playerStats.damage += 6; }
            },
            {
                name: '物理专精',
                description: '物理伤害 +4，武器伤害 +3',
                rarity: 'uncommon',
                effect: () => {
                    this.scene.playerStats.physicalDamage += 4;
                    this.scene.playerStats.weaponDamageBonus += 3;
                }
            },
            {
                name: '魔法专精',
                description: '魔法伤害 +4，元素伤害 +2',
                rarity: 'uncommon',
                effect: () => {
                    this.scene.playerStats.magicalDamage += 4;
                    this.scene.playerStats.elementalDamage += 2;
                }
            },
            {
                name: '真实伤害',
                description: '真实伤害 +2（无视防御）',
                rarity: 'rare',
                effect: () => { this.scene.playerStats.trueDamage += 2; }
            },

            // === 暴击系统升级 ===
            {
                name: '暴击强化',
                description: '暴击率 +8%',
                rarity: 'uncommon',
                effect: () => { this.scene.playerStats.critChance += 0.08; }
            },
            {
                name: '暴击伤害',
                description: '暴击伤害倍数 +0.3',
                rarity: 'uncommon',
                effect: () => { this.scene.playerStats.critDamage += 0.3; }
            },
            {
                name: '暴击专精',
                description: '暴击倍数 +15%，暴击幸运 +5%',
                rarity: 'rare',
                effect: () => {
                    this.scene.playerStats.critMultiplier += 0.15;
                    this.scene.playerStats.criticalLuckBonus += 0.05;
                }
            },

            // === 射击属性升级 ===
            {
                name: '射速提升',
                description: '射击间隔 -80ms，攻击速度 +10%',
                rarity: 'common',
                effect: () => {
                    this.scene.playerStats.fireRate = Math.max(100, this.scene.playerStats.fireRate - 80);
                    this.scene.playerStats.attackSpeed += 0.1;
                }
            },
            {
                name: '射程延长',
                description: '射程 +60，精准度 +2%',
                rarity: 'common',
                effect: () => {
                    this.scene.playerStats.range += 60;
                    this.scene.playerStats.accuracy += 0.02;
                }
            },
            {
                name: '穿透子弹',
                description: '穿透 +1，弹丸速度 +15%',
                rarity: 'uncommon',
                effect: () => {
                    this.scene.playerStats.piercing += 1;
                    this.scene.playerStats.projectileSpeed += 0.15;
                }
            },
            {
                name: '多重射击',
                description: '多重射击 +1',
                rarity: 'rare',
                effect: () => { this.scene.playerStats.multishot += 1; }
            },
            {
                name: '弹射专精',
                description: '弹射次数 +1，弹丸大小 +20%',
                rarity: 'rare',
                effect: () => {
                    this.scene.playerStats.ricochet += 1;
                    this.scene.playerStats.projectileSize += 0.2;
                }
            },

            // === 防御属性升级 ===
            {
                name: '护甲强化',
                description: '物理防御 +4，护甲值 +3',
                rarity: 'common',
                effect: () => {
                    this.scene.playerStats.defense += 4;
                    this.scene.playerStats.armor += 3;
                }
            },
            {
                name: '魔法抗性',
                description: '魔法抗性 +4，状态抗性 +10%',
                rarity: 'uncommon',
                effect: () => {
                    this.scene.playerStats.magicResistance += 4;
                    this.scene.playerStats.statusResistance += 0.1;
                }
            },
            {
                name: '伤害减免',
                description: '伤害减免 +5%',
                rarity: 'uncommon',
                effect: () => { this.scene.playerStats.damageReduction += 0.05; }
            },
            {
                name: '闪避训练',
                description: '闪避率 +10%',
                rarity: 'uncommon',
                effect: () => { this.scene.playerStats.dodgeChance += 0.1; }
            },
            {
                name: '格挡精通',
                description: '格挡率 +8%，格挡值 +3',
                rarity: 'uncommon',
                effect: () => {
                    this.scene.playerStats.blockChance += 0.08;
                    this.scene.playerStats.blockValue += 3;
                }
            },

            // === 护盾系统升级 ===
            {
                name: '护盾生成',
                description: '最大护盾 +20，护盾回复 +2/秒',
                rarity: 'rare',
                effect: () => {
                    this.scene.playerStats.maxShield += 20;
                    this.scene.playerStats.shield = this.scene.playerStats.maxShield;
                    this.scene.playerStats.shieldRegenRate += 2;
                }
            },

            // === 恢复属性升级 ===
            {
                name: '生命回复',
                description: '生命回复 +3/秒',
                rarity: 'uncommon',
                effect: () => { this.scene.playerStats.healthRegen += 3; }
            },
            {
                name: '生命偷取',
                description: '生命偷取 +6%',
                rarity: 'rare',
                effect: () => { this.scene.playerStats.lifeSteal += 0.06; }
            },
            {
                name: '法力偷取',
                description: '法力偷取 +4%，法力回复 +1/秒',
                rarity: 'rare',
                effect: () => {
                    this.scene.playerStats.manaSteal += 0.04;
                    this.scene.playerStats.manaRegen += 1;
                }
            },

            // === 移动属性升级 ===
            {
                name: '移动速度',
                description: '移动速度 +25',
                rarity: 'common',
                effect: () => { this.scene.playerStats.speed += 25; }
            },
            {
                name: '疾风步伐',
                description: '速度倍数 +15%，冲刺速度 +20%',
                rarity: 'uncommon',
                effect: () => {
                    this.scene.playerStats.moveSpeed += 0.15;
                    this.scene.playerStats.sprintSpeed += 0.2;
                }
            },
            {
                name: '瞬移能力',
                description: '瞬移距离 +50，瞬移冷却 -1秒',
                rarity: 'rare',
                effect: () => {
                    this.scene.playerStats.dashDistance += 50;
                    this.scene.playerStats.dashCooldown = Math.max(0, this.scene.playerStats.dashCooldown - 1000);
                }
            },

            // === 武器专精升级 ===
            {
                name: '手枪专精',
                description: '手枪专精 +2，手枪伤害 +15%',
                rarity: 'uncommon',
                effect: () => {
                    this.scene.playerStats.pistolMastery += 2;
                    this.scene.playerStats.weaponDamageBonus += 0.15;
                }
            },
            {
                name: '霰弹枪专精',
                description: '霰弹枪专精 +2，散射范围 +20%',
                rarity: 'uncommon',
                effect: () => {
                    this.scene.playerStats.shotgunMastery += 2;
                    this.scene.playerStats.areaOfEffect += 0.2;
                }
            },
            {
                name: '步枪专精',
                description: '步枪专精 +2，射程 +30，精准度 +5%',
                rarity: 'uncommon',
                effect: () => {
                    this.scene.playerStats.rifleMastery += 2;
                    this.scene.playerStats.range += 30;
                    this.scene.playerStats.accuracy += 0.05;
                }
            },
            {
                name: '机枪专精',
                description: '机枪专精 +2，射速 +20%',
                rarity: 'uncommon',
                effect: () => {
                    this.scene.playerStats.machinegunMastery += 2;
                    this.scene.playerStats.attackSpeed += 0.2;
                }
            },

            // === 特殊效果升级 ===
            {
                name: '击退强化',
                description: '击退力 +2，击退抗性 +15%',
                rarity: 'uncommon',
                effect: () => {
                    this.scene.playerStats.knockback += 2;
                    this.scene.playerStats.knockbackResistance += 0.15;
                }
            },
            {
                name: '范围效果',
                description: '范围效果 +25%，溅射伤害 +3',
                rarity: 'rare',
                effect: () => {
                    this.scene.playerStats.areaOfEffect += 0.25;
                    this.scene.playerStats.splashDamage += 3;
                }
            },
            {
                name: '元素掌握',
                description: '冰冻几率 +8%，燃烧几率 +8%',
                rarity: 'rare',
                effect: () => {
                    this.scene.playerStats.freezeChance += 0.08;
                    this.scene.playerStats.burnChance += 0.08;
                }
            },
            {
                name: '连锁闪电',
                description: '连锁闪电 +1次，元素伤害 +3',
                rarity: 'epic',
                effect: () => {
                    this.scene.playerStats.chainLightning += 1;
                    this.scene.playerStats.elementalDamage += 3;
                }
            },

            // === 资源属性升级 ===
            {
                name: '经验加成',
                description: '经验倍数 +25%',
                rarity: 'common',
                effect: () => { this.scene.playerStats.expMultiplier += 0.25; }
            },
            {
                name: '金币加成',
                description: '金币倍数 +25%',
                rarity: 'common',
                effect: () => { this.scene.playerStats.goldMultiplier += 0.25; }
            },
            {
                name: '寻宝专精',
                description: '寻宝率 +15%，掉落率 +10%',
                rarity: 'uncommon',
                effect: () => {
                    this.scene.playerStats.findTreasureChance += 0.15;
                    this.scene.playerStats.dropRateBonus += 0.1;
                }
            },

            // === 高级属性升级 ===
            {
                name: '吸血术',
                description: '吸血 +8%，生命偷取 +4%',
                rarity: 'rare',
                effect: () => {
                    this.scene.playerStats.vampirism += 0.08;
                    this.scene.playerStats.lifeSteal += 0.04;
                }
            },
            {
                name: '狂暴之力',
                description: '狂暴 +15%（低血量时伤害加成）',
                rarity: 'rare',
                effect: () => { this.scene.playerStats.berserker += 0.15; }
            },
            {
                name: '专注精神',
                description: '专注 +10%（连续命中伤害加成）',
                rarity: 'rare',
                effect: () => { this.scene.playerStats.focus += 0.1; }
            },
            {
                name: '连击大师',
                description: '连击 +1级，攻击速度 +15%',
                rarity: 'epic',
                effect: () => {
                    this.scene.playerStats.combo += 1;
                    this.scene.playerStats.attackSpeed += 0.15;
                }
            },

            // === 生存属性升级 ===
            {
                name: '复活机会',
                description: '复活几率 +5%',
                rarity: 'epic',
                effect: () => { this.scene.playerStats.reviveChance += 0.05; }
            },
            {
                name: '死亡豁免',
                description: '死亡豁免 +8%',
                rarity: 'epic',
                effect: () => { this.scene.playerStats.deathSaveChance += 0.08; }
            },
            {
                name: '伤害反射',
                description: '伤害反射 +12%',
                rarity: 'rare',
                effect: () => { this.scene.playerStats.damageReflection += 0.12; }
            },
            {
                name: '荆棘护甲',
                description: '荆棘伤害 +5，防御 +3',
                rarity: 'rare',
                effect: () => {
                    this.scene.playerStats.thorns += 5;
                    this.scene.playerStats.defense += 3;
                }
            },

            // === 弹药属性升级 ===
            {
                name: '弹药扩容',
                description: '弹药容量 +30%',
                rarity: 'uncommon',
                effect: () => { this.scene.playerStats.ammoCapacity += 0.3; }
            },
            {
                name: '弹药回复',
                description: '弹药回复 +2/秒',
                rarity: 'uncommon',
                effect: () => { this.scene.playerStats.ammoRegenRate += 2; }
            },
            {
                name: '无限弹药',
                description: '无限弹药几率 +10%',
                rarity: 'epic',
                effect: () => { this.scene.playerStats.infiniteAmmoChance += 0.1; }
            },

            // === 技能属性升级 ===
            {
                name: '技能强化',
                description: '技能伤害 +15%，技能范围 +20%',
                rarity: 'rare',
                effect: () => {
                    this.scene.playerStats.skillDamageBonus += 0.15;
                    this.scene.playerStats.skillRange += 0.2;
                }
            },
            {
                name: '冷却缩减',
                description: '技能冷却 -12%',
                rarity: 'rare',
                effect: () => { this.scene.playerStats.skillCooldownReduction += 0.12; }
            },

            // === 超稀有升级 ===
            {
                name: '时间扭曲',
                description: '时间扭曲 +10%（提升游戏速度）',
                rarity: 'legendary',
                effect: () => { this.scene.playerStats.timeDistortion += 0.1; }
            },
            {
                name: '暴击反射',
                description: '暴击反射 +15%',
                rarity: 'legendary',
                effect: () => { this.scene.playerStats.criticalReflection += 0.15; }
            },
            {
                name: '幸运大师',
                description: '幸运加成 +20%，所有几率属性 +5%',
                rarity: 'legendary',
                effect: () => {
                    this.scene.playerStats.luckBonus += 0.2;
                    this.scene.playerStats.critChance += 0.05;
                    this.scene.playerStats.dodgeChance += 0.05;
                    this.scene.playerStats.dropRateBonus += 0.05;
                }
            }
        ];

        // 根据稀有度过滤升级选项
        const weightedUpgrades = [];
        allUpgrades.forEach(upgrade => {
            let weight = 1;
            switch (upgrade.rarity) {
                case 'common': weight = 50; break;
                case 'uncommon': weight = 25; break;
                case 'rare': weight = 10; break;
                case 'epic': weight = 4; break;
                case 'legendary': weight = 1; break;
            }
            
            for (let i = 0; i < weight; i++) {
                weightedUpgrades.push(upgrade);
            }
        });

        // 随机选择3个升级选项
        const selectedUpgrades = [];
        const usedIndexes = new Set();
        
        while (selectedUpgrades.length < 3 && selectedUpgrades.length < weightedUpgrades.length) {
            const randomIndex = Math.floor(Math.random() * weightedUpgrades.length);
            if (!usedIndexes.has(randomIndex)) {
                usedIndexes.add(randomIndex);
                selectedUpgrades.push(weightedUpgrades[randomIndex]);
            }
        }

        return selectedUpgrades;
    }

    // 获取稀有度颜色
    getRarityColor(rarity) {
        switch (rarity) {
            case 'common': return '#ffffff';
            case 'uncommon': return '#1eff00';
            case 'rare': return '#0070dd';
            case 'epic': return '#a335ee';
            case 'legendary': return '#ff8000';
            default: return '#ffffff';
        }
    }

    // 获取稀有度中文名称
    getRarityName(rarity) {
        switch (rarity) {
            case 'common': return '普通';
            case 'uncommon': return '优秀';
            case 'rare': return '稀有';
            case 'epic': return '史诗';
            case 'legendary': return '传说';
            default: return '普通';
        }
    }

    showUpgradeOptions() {
        console.log('显示升级选项');
        this.scene.isUpgrading = true;
        
        // 暂停游戏物理
        if (this.scene.physics) {
            this.scene.physics.pause();
        }

        // 暂停敌人生成计时器
        if (this.scene.spawnEnemyTimer && !this.scene.spawnEnemyTimer.paused) {
            this.scene.spawnEnemyTimer.paused = true;
        }

        // 暂停波数计时器
        if (this.scene.waveTimer && !this.scene.waveTimer.paused) {
            this.scene.waveTimer.paused = true;
        }

        if (this.scene.countdownTimer && !this.scene.countdownTimer.paused) {
            this.scene.countdownTimer.paused = true;
        }

        // 生成升级选项
        const upgradeOptions = this.generateUpgradeOptions();
        this.scene.upgradeOptions = upgradeOptions;

        // 创建升级界面
        this.createUpgradeUI(upgradeOptions);
    }

    createUpgradeUI(upgradeOptions) {
        // 创建半透明背景
        this.upgradeBackground = this.scene.add.rectangle(
            this.scene.cameras.main.centerX, 
            this.scene.cameras.main.centerY, 
            this.scene.cameras.main.width, 
            this.scene.cameras.main.height, 
            0x000000, 
            0.8
        ).setScrollFactor(0);

        // 创建升级选项容器
        this.upgradeContainer = this.scene.add.container(0, 0).setScrollFactor(0);

        // 标题
        const title = this.scene.add.text(
            this.scene.cameras.main.centerX, 
            this.scene.cameras.main.centerY - 150, 
            '选择升级', 
            { 
                fontSize: '32px', 
                fill: '#ffffff', 
                fontFamily: 'Arial' 
            }
        ).setOrigin(0.5).setScrollFactor(0);

        this.upgradeContainer.add(title);

        // 创建升级选项按钮
        const startX = this.scene.cameras.main.centerX - 200;
        const startY = this.scene.cameras.main.centerY - 50;
        
        upgradeOptions.forEach((option, index) => {
            const x = startX + (index * 140);
            const y = startY;

            // 创建选项背景
            const bg = this.scene.add.rectangle(x, y, 120, 150, 0x333333, 0.9)
                .setStrokeStyle(2, this.getRarityColor(option.rarity))
                .setScrollFactor(0);

            // 稀有度标签
            const rarityText = this.scene.add.text(x, y - 60, this.getRarityName(option.rarity), {
                fontSize: '12px',
                fill: this.getRarityColor(option.rarity),
                fontFamily: 'Arial'
            }).setOrigin(0.5).setScrollFactor(0);

            // 选项名称
            const nameText = this.scene.add.text(x, y - 30, option.name, {
                fontSize: '14px',
                fill: '#ffffff',
                fontFamily: 'Arial',
                wordWrap: { width: 110 }
            }).setOrigin(0.5).setScrollFactor(0);

            // 选项描述
            const descText = this.scene.add.text(x, y + 20, option.description, {
                fontSize: '11px',
                fill: '#cccccc',
                fontFamily: 'Arial',
                wordWrap: { width: 110 },
                align: 'center'
            }).setOrigin(0.5).setScrollFactor(0);

            // 使背景可交互
            bg.setInteractive();
            bg.on('pointerdown', () => {
                this.selectUpgrade(option, index);
            });

            // 添加悬停效果
            bg.on('pointerover', () => {
                bg.setFillStyle(0x555555, 0.9);
            });

            bg.on('pointerout', () => {
                bg.setFillStyle(0x333333, 0.9);
            });

            this.upgradeContainer.add([bg, rarityText, nameText, descText]);
        });

        // 添加刷新按钮
        const refreshButton = this.scene.add.rectangle(
            this.scene.cameras.main.centerX, 
            this.scene.cameras.main.centerY + 120, 
            120, 40, 0x4444aa, 0.9
        ).setStrokeStyle(2, 0x6666cc).setScrollFactor(0);

        const refreshText = this.scene.add.text(
            this.scene.cameras.main.centerX, 
            this.scene.cameras.main.centerY + 120, 
            `刷新 (${this.refreshCost}金币)`, 
            { 
                fontSize: '14px', 
                fill: '#ffffff', 
                fontFamily: 'Arial' 
            }
        ).setOrigin(0.5).setScrollFactor(0);

        refreshButton.setInteractive();
        refreshButton.on('pointerdown', () => {
            if (this.scene.gold >= this.refreshCost) {
                this.scene.gold -= this.refreshCost;
                this.refreshCost = Math.floor(this.refreshCost * this.refreshCostMultiplier);
                this.refreshUpgradeOptions();
            }
        });

        this.upgradeContainer.add([refreshButton, refreshText]);
    }

    refreshUpgradeOptions() {
        // 清除现有UI
        this.upgradeContainer.destroy();
        
        // 重新生成选项
        const newUpgradeOptions = this.generateUpgradeOptions();
        this.scene.upgradeOptions = newUpgradeOptions;
        
        // 重新创建UI
        this.createUpgradeUI(newUpgradeOptions);
    }

    selectUpgrade(option, index) {
        console.log('选择升级:', option.name);
        
        // 应用升级效果
        option.effect();
        
        // 清除升级UI
        this.upgradeBackground.destroy();
        this.upgradeContainer.destroy();
        
        // 恢复游戏状态
        this.scene.isUpgrading = false;
        
        // 恢复游戏物理
        if (this.scene.physics) {
            this.scene.physics.resume();
        }

        // 恢复计时器
        if (this.scene.spawnEnemyTimer && this.scene.spawnEnemyTimer.paused) {
            this.scene.spawnEnemyTimer.paused = false;
        }

        if (this.scene.waveTimer && this.scene.waveTimer.paused) {
            this.scene.waveTimer.paused = false;
        }

        if (this.scene.countdownTimer && this.scene.countdownTimer.paused) {
            this.scene.countdownTimer.paused = false;
        }

        // 更新UI显示
        if (this.scene.uiSystem) {
            this.scene.uiSystem.updateAllUI();
        }

        console.log('升级完成，游戏继续');
    }

    // 获取刷新按钮信息
    getRefreshButtonInfo() {
        const canAfford = this.scene.gold >= this.refreshCost;
        return {
            text: `刷新选项 (${this.refreshCost}金币)`,
            canAfford: canAfford,
            cost: this.refreshCost
        };
    }

    // 刷新升级选项
    refreshUpgrades() {
        if (this.scene.gold >= this.refreshCost) {
            // 扣除金币
            this.scene.gold -= this.refreshCost;
            
            // 增加刷新成本
            this.refreshCost = Math.floor(this.refreshCost * this.refreshCostMultiplier);
            
            // 重新生成升级选项
            const newOptions = this.generateUpgradeOptions();
            this.scene.upgradeOptions = newOptions;
            
            // 更新UI显示金币
            if (this.scene.uiSystem) {
                this.scene.uiSystem.updateCoinText(this.scene.gold);
            }
            
            return newOptions;
        }
        return null;
    }

    // 应用临时增益效果
    applyPowerUp() {
        const powerUpEffects = [
            {
                name: '伤害提升',
                effect: () => {
                    const damageBonus = 5;
                    this.scene.playerStats.damage += damageBonus;
                    this.showPowerUpMessage(`伤害 +${damageBonus}`, '#ff6b6b');
                }
            },
            {
                name: '射速提升',
                effect: () => {
                    const speedBonus = 0.2;
                    this.scene.playerStats.attackSpeed += speedBonus;
                    this.showPowerUpMessage(`攻击速度 +${Math.floor(speedBonus * 100)}%`, '#4ecdc4');
                }
            },
            {
                name: '移动速度提升',
                effect: () => {
                    const moveBonus = 30;
                    this.scene.playerStats.speed += moveBonus;
                    this.showPowerUpMessage(`移动速度 +${moveBonus}`, '#95e1d3');
                }
            },
            {
                name: '暴击率提升',
                effect: () => {
                    const critBonus = 0.05;
                    this.scene.playerStats.critChance += critBonus;
                    this.showPowerUpMessage(`暴击率 +${Math.floor(critBonus * 100)}%`, '#f9ca24');
                }
            },
            {
                name: '生命回复',
                effect: () => {
                    const healAmount = 20;
                    this.scene.playerStats.health = Math.min(
                        this.scene.playerStats.maxHealth,
                        this.scene.playerStats.health + healAmount
                    );
                    this.showPowerUpMessage(`生命回复 +${healAmount}`, '#6c5ce7');
                    
                    // 更新生命值显示
                    if (this.scene.uiSystem) {
                        this.scene.uiSystem.updateHealthText(this.scene.playerStats.health);
                    }
                }
            },
            {
                name: '射程延长',
                effect: () => {
                    const rangeBonus = 50;
                    this.scene.playerStats.range += rangeBonus;
                    this.showPowerUpMessage(`射程 +${rangeBonus}`, '#fd79a8');
                }
            },
            {
                name: '经验加成',
                effect: () => {
                    const expBonus = 0.1;
                    this.scene.playerStats.expMultiplier += expBonus;
                    this.showPowerUpMessage(`经验倍数 +${Math.floor(expBonus * 100)}%`, '#fdcb6e');
                }
            }
        ];

        // 随机选择一个增益效果
        const randomEffect = powerUpEffects[Math.floor(Math.random() * powerUpEffects.length)];
        randomEffect.effect();
        
        console.log(`应用临时增益: ${randomEffect.name}`);
    }

    // 显示增益消息
    showPowerUpMessage(message, color = '#ffffff') {
        if (!this.scene.add) return;
        
        const messageText = this.scene.add.text(
            this.scene.cameras.main.centerX,
            this.scene.cameras.main.centerY - 100,
            message,
            {
                fontSize: '20px',
                fill: color,
                fontStyle: 'bold',
                stroke: '#000000',
                strokeThickness: 2
            }
        ).setOrigin(0.5).setScrollFactor(0).setDepth(1000);

        // 添加浮动动画
        this.scene.tweens.add({
            targets: messageText,
            y: messageText.y - 50,
            alpha: 0,
            duration: 2000,
            ease: 'Power2',
            onComplete: () => {
                messageText.destroy();
            }
        });
    }
}